﻿//Author:rikugun
jQuery.fn.extend({
    fillForm: function (options) {
        var settings = jQuery.extend({
            prefix: '' //表单项name前缀  
            , data: {} //数据默认值  
        }, options);
        this.each(function () {
            var that = jQuery(this);
            if (jQuery.inArray(that.attr('tagName').toLowerCase(), ['input', 'select', 'radio', 'textarea']) > -1) {
                that.val(settings.data[that.attr('name')]);
            } else {
                for (var item in settings.data) {
                    try {
                        var o = that.find('[name=\'' + settings.prefix + item + '\']');
                        if (o.attr('type') == 'radio') {
                            $(":radio[name='" + o.attr('name') + "'][value='" + settings.data[item] + "']").attr("checked", "checked");
                        }
                        else if (o.attr('type') == 'checkbox') {
                            var arrCheck = String(settings.data[item]).split('@');
                            $.each(arrCheck, function () {
                                $(":checkbox[name='" + o.attr('name') + "'][value='" + this + "']").attr("checked", "checked");
                            });
                        }
                        else {
                            o.val(settings.data[item] == null ? "" : settings.data[item]);
                        }
                    } catch (e) {
                        if (window.console) {
                            console.error('未能获取元素:' + settings.prefix + item);
                        }
                    }
                }
            }
        });
    }
    , getForm: function (options) {
        var settings = jQuery.extend({
            prefix: '' //表单项name前缀  
            , data: {} //数据默认值  
        }, options);
        var ret = [];
        var forms = this;
        var formatVal = function (val) {
            if (!val) {
                return '';
            } else {
                return val.replace(/\"/g, "\\\"").replace(/\'/g, "\\\'");
            }
        };
        if (this.attr('tagName') == null) {
             forms = this.find('body');
        }
        forms.each(function () {
            var that = jQuery(this);
            var o = {};
            that.find('input,select,radio,textarea').each(function () {
                var el = jQuery(this);
                var fieldFullName = el.attr('name');
                if (fieldFullName == undefined)
                    return;
                var fieldName = fieldFullName.replace(settings.prefix, '');
                if (settings.data[fieldName]) {
                    o[fieldName] = settings.data[fieldName];
                    return;
                }
                if (settings.prefix && fieldFullName.indexOf(settings.prefix) < 0) {
                    //不符合前缀或已设置默认值  
                    return;
                }

                var arrVal = new Array();
                if (el.attr('type') == 'checkbox') {
                    var strArr = '';
                    jQuery('[name="' + fieldFullName + '"]:checkbox:checked').each(function () { arrVal.push(formatVal(jQuery(this).val())); });
                } else if (el.attr('type') == 'radio') {
                    var rad = jQuery('[name="' + fieldFullName + '"]:radio:checked');
                    arrVal.push(formatVal(rad.val()));
                } else {
                    arrVal.push(formatVal(el.val()));
                }
                o[fieldName] = arrVal.toString();
            });
            ret.push(o);
        });
        if (ret.length === 1) return ret[0];
        return ret;
    }

    , clearForm: function (options) {
        var settings = jQuery.extend({
            prefix: '' //表单项name前缀  
            , data: {} //数据默认值  
        }, options);
        var ret = [];
        var forms = this;
        if (this.attr('tagName') == null) { forms = this.find('body'); }
        forms.each(function () {
            var that = jQuery(this);
            var o = {};
            that.find('input,select,radio,textarea').each(function () {
                var el = jQuery(this);
                var fieldFullName = el.attr('name');
                if (fieldFullName.indexOf(settings.prefix) >= 0) {
                    if (el.attr('type') == 'radio' || el.attr('type') == 'checkbox')
                        el.attr('checked', false);
                    else
                        el.val("");
                }
            });
        });
    }
});  